/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.henryschein.mmvvcc.slave;

import com.henryschein.mmvvcc.model.Person;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Alexx
 */
public class GetQuote {
    
    public String get(){
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletCont.getSc());
        EntityManagerFactory entityManagerFactory = (EntityManagerFactory) ctx.getBean("entityManagerFactory");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        List<Person> list = entityManager.createQuery("select p from Person p").getResultList();
        entityManager.getTransaction().commit();
        int randomPosition = (int) (list.size() * Math.random());
        return list.get(randomPosition).getQuote();
    } 
    
}
